'''
Created on Sep 24, 2009

@author: Marco Aurelio Zoqui
'''
import random
from google.appengine.ext import db
from jail import Handler
from jail.grid import DataModelEditor


class City(db.Model):
    code = db.StringProperty(verbose_name='Code')
    name = db.StringProperty(verbose_name='Name')
    population = db.IntegerProperty(verbose_name='Population')
    doctors = db.IntegerProperty(verbose_name='Doctors')

class Contact(db.Model):
    name = db.StringProperty(verbose_name='Name')
    city = db.ReferenceProperty(City, required=False,verbose_name='City Name')        
    phone = db.StringProperty(verbose_name='Main Phone',required=True)
    phonetype = db.StringProperty(choices=set(["WORK","HOME","MOBILE","VOIP","PAGER","NOT APPLICABLE"]),verbose_name='Phone Type',default='WORK')


class CityGridHandlerNoTemplate(DataModelEditor):
    def __init__(self):
        self.model = City
        self.model.code.width = '100px'
        self.model.name.width = '250px'
        self.model.population.width = '70px'
        self.model.population.align = 'right'
        self.model.population.mathparser=True
        self.model.population.calcsum=True
        self.model.doctors.width = '70px'
        self.model.doctors.align = 'right'
        self.model.doctors.mathparser=True
        self.model.doctors.calcsum=True
        return


class CityGridHandler(CityGridHandlerNoTemplate):
    def __init__(self):
        super(CityGridHandler,self).__init__()
        self.templatePath = '/templates/grid1.html'
        self.templateData = {'title' : 'Cities'}
        return
    

class ContactGridHandlerNoTemplate(DataModelEditor):
    def __init__(self):
        self.model = Contact
        self.model.name.width = '200px'
        self.model.phone.width = '100px'
        self.model.phone.align = 'center'
        self.model.phonetype.width = '120px'
        self.model.city.referenceHelpField = 'name'
        self.model.city.width = '120px'
        self.model.city.protected = {
                                         'population': {
                                              'width':'70px',
                                              'align':'right',
                                              'calcsum':True
                                         },
                                         'code': {
                                              'width':'30px',
                                              'align':'center'
                                         },
                                         'doctors': {
                                              'width':'50px',
                                              'align':'center',
                                              'calcsum':True
                                         } 
                                    }

        return

class ContactGridHandler(ContactGridHandlerNoTemplate):
    def __init__(self):
        super(ContactGridHandler,self).__init__()
        self.templatePath = '/templates/grid1.html'
        self.templateData = {'title' : 'Contacts'}
        return

class Index(Handler):
    target = [r'populate',r'deleteall']
    def add(self,citcode,citname,conname,phonetype,phone,popu,doct):
        city = City(code=citcode,name=citname,population=popu,doctors=doct)
        city.put()
        contact = Contact(name=conname,phonetype=phonetype,city=city,phone=phone)
        contact.put()

    def delAll(self):
        for contact in Contact.all():
            contact.delete()
        for city in City.all():
            city.delete()
        return
    
    def insAll(self):
        self.add('CGH','SAO PAULO','NANA VASCONCELOS','WORK','6545-4596',11037593,10)
        self.add('FLN','FLORIANOPOLIS','VICTOR MEIRELES','WORK','4577-4596',1299283,20)
        self.add('RIO','RIO DE JANEIRO','TOM JOBIM','PAGER','6265-7466',5600000,30)
        self.add('BHZ','BELO HORIZONTE','LO BORGES','HOME','1245-7894',5054000,40)
        self.add('POA','PORTO ALEGRE','KLEDIR RAMIL','WORK','3345-7556',1440939,30)
        self.add('BSB','BRASILIA','RENATO RUSSO','HOME','6244-6896',2000000,20)
        self.add('SSA','SALVADOR','DORIVAL CAIMMY','HOME','6464-7856',2100000,10)
        self.add('MIA','MARILIA','MATSUDA','NOT APPLICABLE','7475-4896',224093,30)
        self.add('CPQ','CAMPINAS','CARLOS GOMES','MOBILE','6256-4666',3200000,5)
        return
    
    def get(self):
        if (self.request.uri.endswith('deleteall')):
            self.delAll()
        else:
            if (self.request.uri.endswith('populate')) and (Contact.all().count(1)==0):
                self.insAll()
 
        self.go('/demo/contactgridhandler')  


